#include "usuariodialog.h"
#include "ui_usuariodialog.h"


UsuarioDialog::UsuarioDialog( UsuarioClass obj, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UsuarioDialog)
{
    ui->setupUi(this);
    Usuario = obj;

    UsuarioDialog::setWindowTitle("@"+Usuario.getNome());
    ui->NomeLabel->setText("@"+Usuario.getNome());
    atualizarJanela();
}

UsuarioDialog::~UsuarioDialog()
{
    delete ui;

}

void UsuarioDialog::atualizarJanela()
{
    ApiTwitter api;
    QSqlQuery query = api.getUserTimeline(Usuario.getId());

    ui->saidaTimelineTextEdit->clear();

    while(query.next())
    {
        ui->saidaTimelineTextEdit->insertPlainText(
                    "@"+ Usuario.getNome()+" -> " + query.value(2).toString()+ "\n" +
                    "tweetado em : "+query.value(0).toString()+ "\n\n");
    }

}

void UsuarioDialog::atualizarCont()
{
    int tamanho = ui->textoTweetTextEdit->toPlainText().length();
    tamanho = 140-tamanho;

    QString x = QString::number(tamanho);
    ui->contTwittarLabel->setText(x);

    if(tamanho > 0)
    {
        ui->contTwittarLabel->setStyleSheet("QLabel{ color: #000000; }");

    }
    else
    {
        ui->contTwittarLabel->setStyleSheet("QLabel{ color: red; }");
    }

}

void UsuarioDialog::on_atualizarTimelineButton_clicked()
{

    ApiTwitter api;
    QSqlQuery query = api.getUserTimeline(Usuario.getId());

    ui->saidaTimelineTextEdit->clear();

    while(query.next())
    {
        ui->saidaTimelineTextEdit->insertPlainText(
                    "@"+ Usuario.getNome()+" -> " + query.value(2).toString()+ "\n" +
                    "tweetado em : "+query.value(0).toString()+ "\n\n");
    }

}

void UsuarioDialog::on_pushButton_clicked()
{
    QString tweet = ui->textoTweetTextEdit->toPlainText();

    if(tweet.length() > 0 && tweet.length() <= 140)
    {
        ApiTwitter api;
        api.newTweet(Usuario.getId(), tweet);
        ui->contTwittarLabel->setText("140");
        ui->textoTweetTextEdit->clear();
        QMessageBox::information(NULL, "Confirmação", "Tweet enviado com sucesso!");
    }
    else
    {
        QMessageBox::information(NULL, "ERRO", "Tweet precisa ter 140 caracteres!");
    }

}

void UsuarioDialog::on_UsuarioDialog_finished(int result)
{

}
